package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.risk.BpReportSettingDto;
import com.joysuch.wwyt.bp.bean.risk.BpTeamBean;
import com.joysuch.wwyt.bp.entity.BpDepart;
import com.joysuch.wwyt.bp.entity.risk.BpTeam;
import com.joysuch.wwyt.bp.service.BpTeamService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List;

/**
 * @Author ZhangDong
 * @Date 2020/3/4 11:45
 */
@RestController
@RequestMapping("/bp/risk/team")
@Api(tags = "/bp/risk/team 风险研判-班组级")
@CrossOrigin
public class BpRiskTeamController {

    @Autowired
    private BpTeamService bpTeamService;

    @PostMapping("save")
    @RequiresPermissions("bp:risk:team:save")
    @ApiOperation(value = "上报/修改")
    public ResultBean save(@RequestBody @Valid BpTeamBean bpTeamBean) {
        return bpTeamService.save(bpTeamBean);
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "根据id查询单条数据 & 审批", response = BpReportSettingDto.class)
    public ResultBean getById(@PathVariable Long id) {
        return bpTeamService.getById(id);
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询 reportDate: 上报日期", response = BpTeam.class)
    public ResultBean page(Pageable pageable, @RequestParam(value = "reportDate", required = false) LocalDate reportDate) {
        return bpTeamService.page(pageable, reportDate, 0);
    }

    @GetMapping("page/upper")
    @ApiOperation(value = "分页查询--部门级 reportDate: 上报日期  flag传2", response = BpTeam.class)
    public ResultBean page(Pageable pageable, @RequestParam(value = "reportDate", required = false) LocalDate reportDate,
                           @RequestParam(value = "flag") Integer flag) {
        return bpTeamService.page(pageable, reportDate, flag);
    }

    @GetMapping("submit/{id}")
    @ApiOperation(value = "提交")
    @RequiresPermissions("bp:risk:team:submit")
    public ResultBean submit(@PathVariable Long id) {
        return bpTeamService.submit(id);
    }

    @GetMapping("delete/{id}")
    @RequiresPermissions("bp:risk:team:delete")
    @ApiOperation(value = "删除")
    public ResultBean delete(@PathVariable Long id) {
        return bpTeamService.delete(id);
    }

    @GetMapping("verify/{id}/{flag}")
    @RequiresPermissions("bp:risk:team:verify")
    @ApiOperation(value = "审核通过/不通过 flag: 0 不通过; 1 通过")
    public ResultBean delete(@PathVariable Long id, @PathVariable int flag) {
        return bpTeamService.verify(id, flag);
    }

    @GetMapping("getDepart/{flag}")
    @ApiOperation(value = "获取所在班组/上级部门  flag: 0 所在班组; 1 所在班组上级部门", response = BpDepart.class)
    public ResultBean getDepart(@PathVariable int flag) {
        return bpTeamService.getDepart(flag);
    }

    @GetMapping("/getLastTeam")
    @ApiOperation("获取当前班组最近的一次记录")
    public ResultBean getLastTeam() {
       return bpTeamService.getLastTeam();
    }

    @PostMapping("work/calculate")
    @ApiOperation("班组作业计算汇总")
    public ResultBean workCalculate(@RequestBody List<Long> teamIds){
        return bpTeamService.workCalculate(teamIds);
    }
}
